<#
文档索引一致性检查（原生 PowerShell）
用途：检查 docs/meta_index.md 是否索引了关键文档；支持 --fix 自动追加缺失条目。
用法：
  pwsh -File scripts/windows/check-doc-sync.ps1 [--fix]
#>
param([switch]$fix)

$WindowsDir = Split-Path -Parent $PSCommandPath
$ScriptsDir = Split-Path -Parent $WindowsDir
$RepoDir = Split-Path -Parent $ScriptsDir

$metaIndex = Join-Path $RepoDir 'docs/meta_index.md'
$rulesProject = Join-Path $RepoDir 'rules/project_rules.md'
$frameworkChanges = Join-Path $RepoDir 'docs/framework_changes.md'

if (!(Test-Path $metaIndex)) { Write-Error "缺少索引文件：$metaIndex"; exit 2 }

$content = Get-Content -Raw $metaIndex
$missing = @()
if ($content -notmatch [regex]::Escape('rules/project_rules.md')) { $missing += 'rules/project_rules.md' }
if ($content -notmatch [regex]::Escape('docs/framework_changes.md')) { $missing += 'docs/framework_changes.md' }

if ($missing.Count -gt 0) {
  Write-Warning "meta_index.md 缺失：`n - " + ($missing -join "`n - ")
  if ($fix) {
    $linesToAdd = "`n# 自动追加（AI 脚本）`n" + ($missing | ForEach-Object { "- $_" }) -join "`n"
    Add-Content -Path $metaIndex -Value $linesToAdd
    Write-Host "已追加缺失索引条目"
    exit 0
  } else { exit 2 }
} else {
  Write-Host "文档索引一致性检查通过"
  exit 0
}
